package com.class5.springboot.htmlcontroller;

import com.class5.springboot.entity.TbXtUser;
import com.class5.springboot.entity.vo.TbXtUserVo;
import com.class5.springboot.frame.web.SuperRestController;
import com.class5.springboot.frame.web.result.ResultData;
import com.class5.springboot.service.facade.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class HLoginController extends SuperRestController {

    @Autowired
    private ILoginService loginService;

    @RequestMapping("/")
    public String index() {
        return "/login";
    }

    @RequestMapping("/html/login")
    @ResponseBody
    public ResultData login(HttpServletRequest req) {
        String username = req.getParameter("userName");
        String password = req.getParameter("passWord");
//        String vcode = param.getString("vcode");
//        String vcodekey = param.getString("vcodekey");
        log.info("username:{}, password:{}, vcode:{}, vcodekey:{}", username, password);
//        checkNotEmpties(username,"帐号", password,"密码");
//        if(!codeBean.checkImageCode(vcodekey, vcode)) {
//            throw new AppException("验证码不正确");
//        }
        TbXtUserVo xtUserVo = loginService.login(username,password,getRealIp(req));
        HttpSession session = req.getSession();
        session.setAttribute("xtUser",xtUserVo);
        return successWraper(xtUserVo);
    }

    @RequestMapping("/html/exit")
    @ResponseBody
    public ResultData exit(HttpServletRequest req) {
        log.info("退出登录");
        HttpSession session = req.getSession();
        session.invalidate();
        return successWraper("退出登录");
    }

}
